<?php

/**
 * This is the model class for table "zy_rebatecommission".
 *
 * The followings are the available columns in table 'zy_rebatecommission':
 * @property integer $id
 * @property integer $agent_company_id
 * @property integer $user_id
 * @property integer $fandian_rate
 * @property integer $fandian_id
 * @property string $money
 * @property integer $type
 * @property string $date
 * @property integer $number
 */
class Rebatecommission extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Rebatecommission the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_rebatecommission';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('agent_company_id, user_id, fandian_rate, fandian_id, type, number', 'numerical', 'integerOnly'=>true),
			array('money', 'length', 'max'=>11),
			array('date', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, agent_company_id, user_id, fandian_rate, fandian_id, money, type, date, number', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('base','Commission Number'),
			'agent_company_id' => Yii::t('base','Agent Company'),
			'user_id' => Yii::t('base','Agent Name'),
			'fandian_rate' => Yii::t('base','Fandian Rate'),
			'fandian_id' => Yii::t('base','Fandian'),
			'money' => Yii::t('base','Money'),
			'type' => Yii::t('base','type'),
			'date' => Yii::t('base','Date'),
			'number' => Yii::t('base','Number'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('agent_company_id',$this->agent_company_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('fandian_rate',$this->fandian_rate);
		$criteria->compare('fandian_id',$this->fandian_id);
		$criteria->compare('money',$this->money,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('number',$this->number);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getAgentCompanyId(){
		$user_id=yii::app()->user->id;
		$agentperson=AgentPerson::model()->find('user_id='.$user_id);
		$company_number=$agentperson->company_number;
		$agent=AgentCompany::model()->find('agent_number='.$company_number);
		return $agent;
	}
	public function getAllagentId(){
		$sql="SELECT * FROM zy_agent_company a LEFT JOIN zy_user b ON a.user_id=b.user_id";
		$rs=Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($rs,'agent_id','user_name');
	}
	public function gettype($id){
		if($id==1){
			echo "返点";
		}elseif($id==2){
			echo "提成";
		}
	}
	public function createRebatecommission(){
	$agent_company_id=$_POST['agent_company_id'];
		$user_id=$_POST['user_id'];
		$date=$_POST['date'];
		$fandian_rate=$_POST['rate'];
		$fandian_id=$_POST['fandian_id'];
		$money1=$_POST['package_price'];
		$money2=$_POST['bonus'];
		$number=$_POST['number'];
		$type1=1;
		$type2=2;
		if($_POST['number']!=""){
		$sql="Insert into zy_rebatecommission (agent_company_id,user_id,fandian_rate,fandian_id,money,type,date,number) values (:agent_company_id,:user_id,:fandian_rate,:fandian_id,:money,:type,:date,:number)";
		$result = yii::app()->db->createCommand($sql);
		$result->bindParam(":agent_company_id",$agent_company_id);
		$result->bindParam(":user_id",$user_id);
		$result->bindParam(":fandian_rate",$fandian_rate);
		$result->bindParam(":fandian_id",$fandian_id);
		$result->bindParam(":money",$money1);
		$result->bindParam(":type",$type1);
		$result->bindParam(":date",$date);
		$result->bindParam(":number",$number);
		$result->execute();
		
		$fandian_id2=0;
		$fandian_rate2=0;
		$number2=0;
		$sql2="Insert into zy_rebatecommission (agent_company_id,user_id,fandian_rate,fandian_id,money,type,date,number) values (:agent_company_id,:user_id,:fandian_rate,:fandian_id,:money,:type,:date,:number)";
		$result2 = yii::app()->db->createCommand($sql2);
		$result2->bindParam(":agent_company_id",$agent_company_id);
		$result2->bindParam(":user_id",$user_id);
		$result2->bindParam(":fandian_rate",$fandian_rate2);
		$result2->bindParam(":fandian_id",$fandian_id2);
		$result2->bindParam(":money",$money2);
		$result2->bindParam(":type",$type2);
		$result2->bindParam(":date",$date);
		$result2->bindParam(":number",$number2);
		$result2->execute();
		
		return true;
		}

	}
	public function createsaleRebatecommission(){
		$user_id=$_POST['user_id'];
		$date=$_POST['date'];
		$fandian_rate=$_POST['rate'];
		$money1=$_POST['sum_package_price'];
		$number=$_POST['number'];
		$type1=1;
		if($_POST['number']!=""){
			$sql="Insert into zy_rebatecommission (user_id,fandian_rate,money,type,date,number) values (:user_id,:fandian_rate,:money,:type,:date,:number)";
			$result = yii::app()->db->createCommand($sql);
			$result->bindParam(":user_id",$user_id);
			$result->bindParam(":fandian_rate",$fandian_rate);
			$result->bindParam(":money",$money1);
			$result->bindParam(":type",$type1);
			$result->bindParam(":date",$date);
			$result->bindParam(":number",$number);
			$result->execute();
			return true;
		}
	}
	public function getagentcompany(){
		if($_POST['Rebatecommission']!=""){
			$agent_company_id=$_POST['Rebatecommission']['agent_company_id'];
			if($agent_company_id!=""){
				$agentcompany=AgentCompany::model()->find('agent_id='.$agent_company_id);
				return $agentcompany;
			}
		}
	}
	public function getmoney1($company_number){
		if($_POST['Rebatecommission']!=""){
		if($company_number!=""){
		$sql="SELECT SUM(package_price) FROM zy_bonus where company_number=".$company_number;
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$result = $command->queryAll();
		for($i=0;$i<count($result);$i++){
			$results[$i]=$result[$i]['SUM(package_price)'];
		}
		$money1=$results[0];
		return $money1;
		}
		}
	}
	public function getmoney2($company_number){
		if($_POST['Rebatecommission']!=""){
		if($company_number!=""){
		$sql2="SELECT SUM(bonus) FROM zy_bonus where company_number=".$company_number;
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql2);
		$result2 = $command->queryAll();
		for($i=0;$i<count($result2);$i++){
			$results2[$i]=$result2[$i]['SUM(bonus)'];
		}
		$money2=$results2[0];
		return $money2;
		}
		}
	}
	public function getcount($company_number){
		if($_POST['Rebatecommission']!=""){
		$date=$_POST['Rebatecommission']['date'];
		if($company_number!="" && $date!=""){
		$count=Bonus::model()->find("company_number=$company_number AND sale_time like '$date%' ")->count();
		$number=$count;
		return $number;
		}
		}
	}
	public function getfandian($agent_type,$number){
		if($_POST['Rebatecommission']!=""){
			if($number!="" && $agent_type!=""){
			$fandian=Fandian::model()->find("agent_type=".$agent_type." AND is_delete =0 AND $number between min and max");
			return $fandian;
			}
		}
	}
	public function setagentcompanyId(){
		$user_id=yii::app()->user->id;
		$agentcompany=AgentCompany::model()->find('user_id='.$user_id);
		$agent_company_id=$agentcompany->agent_id;
		return $agent_company_id;
	}
	public function getcommissionNumber($agent_company_id,$user_id,$date){
		$rs=Rebatecommission::model()->find("agent_company_id=".$agent_company_id." AND user_id=".$user_id." AND date="."'$date'"."  AND type=2");
		return $rs->money;
	}
	public function getAmount($money,$agent_company_id,$user_id,$date){
		$rs=Rebatecommission::model()->find("agent_company_id=".$agent_company_id." AND user_id=".$user_id." AND date="."'$date'"."  AND type=2");
		$amount=$rs->money+$money;
		return $amount;
	}
	
}